'use strict';
/* Controllers */
app.controller('memberListCtrl', ['$scope', '$http', '$state', 'apiService', '$cookieStore', '$timeout','toaster','$rootScope','ngDialog', function ($scope, $http, $state, apiService, $cookieStore, $timeout,toaster,$rootScope,ngDialog) {
    $scope.search_obj = {
        username: '',
        level: '',
        company: '',
        page: 1,
        rows: 10
    };

    $scope.paginationConf = {
        currentPage: 1,
        totalItems: 66,
        itemsPerPage: $scope.search_obj['rows'],
        perPageOptions: [10, 20, 30, 40, 50],
        onChange: function () {
            $scope.search_obj['page'] = $scope.paginationConf.currentPage;
            // 获取非会员列表
            apiService.post({
                modelName: 'member',
                apiName: 'getMemberByLike'
            }, $scope.search_obj, function (data) {
                if (data.code == 200) {
                    $scope.pageData = data['data']['list'];
                    $scope.paginationConf.totalItems = data['data']['total']? data['data']['total']:0;
                } else {
                    toaster.pop('error', '提示', data['msc']);
                }
            }, function (err) {
                toaster.pop('error', '提示', '服务器出小差咯...');
            });
        }
    };


    // $scope.$watch('search_obj',function(){
    //     $scope.paginationConf.onChange();
    // },true);

    $scope.searchFun = function () {
        $scope.paginationConf.onChange();
    }


    // 修改等级
    $scope.upUserLevel = function (item) {
        // 对话框
        $scope.dialog = {
            title: '提示',
            content: '选择用户等级?',
            previous: $rootScope.userObj['previous'],
            level: item.level
        };

        ngDialog.openConfirm({
            template: '/tpl/dialog.html',
            className: 'ngdialog-theme-plain',
            scope: $scope
        }).then(function (value) {
            if (value['statu'] == true) {
                if (value['val']) {
                    item['level'] = value['val'];
                }
                apiService.post({
                    modelName: 'member',
                    apiName: 'upmember'
                }, item, function (data) {
                    if (data.code == 200) {
                        toaster.pop('success', '提示', data['msc']);
                    } else {
                        toaster.pop('error', '提示', data['msc']);
                    }
                }, function (err) {
                    toaster.pop('error', '提示', '服务器出小差咯...');
                });
            }
        });

    }

    // 修改冻结状态
    $scope.upUserFreeze = function (item, freeze) {
        $scope.btn_disabled = true;
        item['freeze'] = freeze;
        apiService.post({
            modelName: 'member',
            apiName: 'upmember'
        }, item, function (data) {
            if (data.code == 200) {
                toaster.pop('success', '提示', data['msc']);
            } else {
                toaster.pop('error', '提示', data['msc']);
            }
            $scope.btn_disabled = false;
        }, function (err) {
            $scope.btn_disabled = false;
            toaster.pop('error', '提示', '服务器出小差咯...');
        });

    }

}]);